package com.example.demo.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * @author lyc
 * @date 2019/3/27.
 */
public class PinyinUtil {

    public static String getPinYinHeadChar(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

    public static void main(String[] args) {
        String str = PinyinUtil.getPinYinHeadChar("雪花勇闯天涯/有奖啤酒/瓶装12*580ml(件)".replace("/","").toLowerCase());
        System.out.println(str);
    }
}